//获取url中的参数
function getUrlParam(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
  var r = window.location.search.substr(1).match(reg);  //匹配目标参数
  if (r != null) return unescape(r[2]); return null; //返回参数值
}

function LoadPage(){

  this.show = function(parent){
    var content = $('<div width="100%" align="center" id="blink" class="loading0"><br></br>loading......</div>');
    parent.append(content);
    blinklink();
  }

  this.hide = function(parent){
    var page = $('#blink',parent);
    if(page != null)
      page.remove();
  }
}

function blinklink() {
  var max = 24;
  for(var i = 0; i < max; i++)
    if ($('#blink').attr('class') == 'loading'+i) {
      if(i < max-1)
        $('#blink').attr('class','loading'+(i+1));
      else
        $('#blink').attr('class','loading0');
      max = 0;
    }
  timer = setTimeout("blinklink()", 100);
}
function stoptimer() {
  clearTimeout(timer);
}

function BreadCrumb(){
  var _data = [];//action:title
  var _container = null;
  var _conId = '';

  this.setData = function(data){
    _data = data;
  }

  this.getData = function(){
    return _data;
  }

  this.setContainer = function (container) {
    _container = container;
    if(_container.attr('id')!=null && _container.attr('id')!='')
      _conId = _container.attr('id');
  }

  this.getContainer = function () {
    return _container;
  }

  this.setConId = function (conId) {
    _conId = conId;
  }

  this.getConId = function () {
    return _conId;
  }

  this.create = function(){
    if(_conId != null && _conId != '' && _container.attr('id')!=null && _container.attr('id')!='')
      _container.attr('id',_conId);
    _container.empty();
    for(var i=0;i<_data.length;i++) {
      var li = $('<span id="' + _conId + '_bc_'+i+'" onclick="breadCrumb.go('+i+');' + _data[i].action + '"> / ' + _data[i].title + '</span>');
      _container.append(li);
    }
  }

  this.add = function(element){
    var li = $('<span id="' + _conId + '_bc_'+_data.length+'" onclick="breadCrumb.go('+_data.length+');' + element.action + '"> / ' + element.title + '</span>');
    _container.append(li);
    _data.push(element);
  }

  this.go = function(index){
    if(index<_data.length-1) {
      for(var i=index+1;i<_data.length;i++)
       $('#' + _conId + '_bc_'+i).remove();
      _data.splice(index, _data.length - index - 1);
    }
  }

  this.goBack = function(){
    if(_data.length > 1) {
      $('#' + _conId + '_bc_'+(_data.length - 2)).click();
    }
  }
}

function BoxCard(){
  var _id = '';
  var _className = [];
  var _style = null;
  var _src = null;
  var _srcType = 0;
  var _parentElement = null;
  var _div = null;
  var _content = null;

  this.setId = function(id)
  {
    _id = id;
  }

  this.getId = function()
  {
    return _id;
  }

  this.addClass = function(className)
  {
    _className.push(className);
  }

  this.getClass = function()
  {
    return _className;
  }

  this.setStyle = function(style)
  {
    _style = style;
  }

  this.getStyle = function()
  {
    return _style;
  }

  this.setSrc = function(src)
  {
    _src = src;
    try{
    if(_src.indexOf('//')==0)
      _src = '../../' + _src.substr(2);
    }catch (e)
    {}
  }

  this.getSrc = function()
  {
    return _src;
  }

  this.setSrcType = function(srcType)
  {
    _srcType = srcType;
  }

  this.getSrcType = function()
  {
    return _srcType;
  }

  this.setParentElement = function(parentElement)
  {
    _parentElement = parentElement;
  }

  this.getParentElement = function(){
    return _parentElement;
  }

  this.create = function(){
    var div = $('<div id="'+this.getId()+'" bgcolor="#444444"></div>');
    div.addClass('box-card');
    var content = null;
    if(this.getSrcType()==0) {
      content = $('<iframe id="frm_'+this.getId()+'"></iframe>');
      content.addClass('box-card-border');
      content.attr('src', this.getSrc());
      div.append(content);
    }
    else{
      content = $('<div></div>');
      content.addClass('box-card-border');
      content.append(this.getSrc());
      div.append(content);
    }
    for(var i in this.getClass()) {
      content.addClass(this.getClass()[i]);
    }
    for(var key in this.getStyle()){
      if(key == 'top' || key == 'left' || key == 'width' || key == 'height' || key == 'position')
        div.css(key,this.getStyle()[key]);
      else
        content.css(key,this.getStyle()[key]);
    }

    _content = content;
    _div = div;

    this.getParentElement().append(div);
  }

  this.getContent = function()
  {
    return _content;
  }

  this.remove = function()
  {
    _div.remove();
  }
}

function createBoxCardMenuDown(id,menuList,depend,width)
{
  var top = depend.offset().top + depend.height();
  var totalWidth = $(document.body).width();
  var left = depend.offset().left;
  if(totalWidth-left<width)
    left -= width - totalWidth + left;
  return createBoxCardMenu(id,menuList,top,left, width);
}

function createBoxCardMenu(id,menuList,top,left, width)
{
  var content = $('<ul class="nav nav-pills nav-stacked"></ul>');
  for(var i=0;i<menuList.length;i++){
    var li = $('<li onclick="'+menuList[i].action+'">'+menuList[i].title+'</li>');
    content.append(li);
  }
  var style = {
    'top':top,
    'left':left,
    'width': width,
    'background':'#444444',
    'position':'absolute'
  };
  var boxCard = new BoxCard();
  boxCard.setId(id);
  boxCard.setSrc(content);
  boxCard.addClass('button');
  boxCard.setSrcType(1);
  boxCard.setStyle(style);
  var topBody = $('body');
  boxCard.setParentElement(topBody);
  boxCard.create();
  return boxCard;
}

function createBoxCardAbs(src,width,height,top,left,srcType)
{
  var style = {
    'width':width,
    'height':height,
    'top':top,
    'left':left,
    'position':'absolute'
  };
  var boxCard = new BoxCard();
  boxCard.setSrc(src);
  boxCard.setSrcType(srcType);
  boxCard.setStyle(style);
  var topBody = getMainBody(window);
  boxCard.setParentElement(topBody);
  boxCard.create();
}

function runMainScript(script)
{
  var test = script.split('.').length;
  if(test<10){
    var script = 'parent.'+ script;
    try {
      eval(script);
    }
    catch(e)
    {
      runMainScript(script);
    }
  }
}

function getMainElement(eleId)
{
  try {
    if ($('#'+eleId, window.parent.document) != null)
      return $('#'+eleId, window.parent.document);
    else {
      return getMainBody(win.parent);
    }
  }
  catch(e)
  {
    try {
      return $('#' + eleId);
    }catch (ee)
    {
      return null;
    }
  }
}

function getMainBody(win)
{
  try {
    if ($('#mainBody', window.parent.document) != null)
      return $('#mainBody', window.parent.document);
    else {
      return getMainBody(win.parent);
    }
  }
  catch(e)
  {
    return $('body');
  }
}

function hateoasGetUrl(links,rel)
{
  var url = null;
  for(var i=0;i<links.length && url == null;i++)
    if(links[i].rel == rel)
      url = links[i].href;
  return url;
}
